package com.zlt.bkparking.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zlt.bkparking.dto.AdQueryParams;
import com.zlt.bkparking.dto.ExportAdvertisementPlayedExcel;
import com.zlt.bkparking.entity.AdvertisementPlayed;
import com.zlt.bkparking.mapper.AdvertisementPlayedMapper;
import com.zlt.bkparking.service.AdvertisementPlayedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Date: 2022/10/6 21:14
 * @Author: hzj
 * @Description:
 */
@Service
public class AdvertisementPlayedServiceImpl implements AdvertisementPlayedService {
    @Autowired
    private AdvertisementPlayedMapper advertisementPlayedMapper;


    @Override
    public PageInfo<AdvertisementPlayed> page(AdQueryParams params) {
        PageHelper.startPage(params.getPage(),params.getLimit());
        List<AdvertisementPlayed> list = advertisementPlayedMapper.selectPage(params.getKey(), params.getStartDate(), params.getEndDate());
        PageInfo<AdvertisementPlayed> pageInfo = new PageInfo<>(list);
        pageInfo.setList(list);
        return pageInfo;
    }
    @Override
    public void exportExcel(AdQueryParams params, HttpServletResponse response) {
        List<AdvertisementPlayed> list = advertisementPlayedMapper.selectPage(params.getKey(), params.getStartDate(), params.getEndDate());

        List<ExportAdvertisementPlayedExcel> collect = list.stream().map(o -> {
            ExportAdvertisementPlayedExcel excel = new ExportAdvertisementPlayedExcel();
            excel.setAdName(o.getAdvertisement().getAdName());
            excel.setEId(o.geteId());
            excel.setApStart(o.getApStart());
            excel.setSpEnd(o.getApEnd());
            excel.setAuCompany(o.getUser().getAuCompany());
            return excel;
        }).collect(Collectors.toList());

        // 定义excel的页签
        List<ExportAdvertisementPlayedExcel> excels = new ArrayList<>();

        // 字节数组输出流
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        // 构建写入对象
        ExcelWriter excelWriter = EasyExcel.write(outputStream).build();

        // 这里可以多个WriteSheet对象
        WriteSheet user = EasyExcel.writerSheet(0, "播放记录").head(ExportAdvertisementPlayedExcel.class).build();

        // 这里可以写多个write
        excelWriter.write(collect, user);

        // 写入完成
        excelWriter.finish();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        String fileName = format + "播放记录.xlsx";

        try {
            outputStream.writeTo(response.getOutputStream());
            response.setContentType("application/octet-stream");
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"; filename*=utf-8''%s", fileName, fileName));
            response.setContentLengthLong(outputStream.size());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean save(AdvertisementPlayed advertisementPlayed) {
        return advertisementPlayedMapper.insert(advertisementPlayed) > 0;
    }
}
